const mongoose = require('mongoose')
const { Schema, model } = mongoose

// 文档schema
const userSchema = new Schema({
    __v: {
        type: Number,
        select: false
    },
    name: {
        type: String, // 传入的字段都会转为string
        required: true // 必填
    },
    password: {
        type: String,
        required: true,
        select: false // 列表中不返回该字段
    },
    // 头像
    avatar_url: {
        type: String,
    },
    // 性别
    gender: {
        type: String,
        enum: ['male', 'female'], // 枚举
        default: 'male',
        required: true
    },
    // 一句话介绍
    hideline: { type: String },
    // 居住地
    // 引用：存储的时候是一个id,但是可以通过populate的方法替换为一个Topic对象
    locations: {
        type: [
            {
                type: Schema.Types.ObjectId,
                ref: 'Topic'
            }
        ],
        select: false
    },
    // 行业
    business: {
        type: Schema.Types.ObjectId,
        ref: 'Topic',
        select: false
    },
    // 职业经历
    employments: {
        type: [{
            company: {
                type: Schema.Types.ObjectId,
                ref: 'Topic'
            },
            job: {
                type: Schema.Types.ObjectId,
                ref: 'Topic'
            },
        }],
        select: false
    },
    // 教育经历
    educations: {
        type: [{
            school: {
                type: Schema.Types.ObjectId,
                ref: 'Topic'
            },
            major: {
                type: Schema.Types.ObjectId,
                ref: 'Topic'
            }, //专业
            diploma: { type: Number, enum: [1, 2, 3, 4, 5] }, // 学历
            entrance_year: { type: Number }, // 入学年份
            graduation_year: { type: Number } // 毕业年份
        }],
        select: false
    },
    // 关注
    following: {
        // ref 引用
        // 可以基于这个id来查询与其对于的用户的属性
        type: [{ type: Schema.Types.ObjectId, ref: 'User' }],
        select: false
    },
    // 关注话题
    followingTopics: {
        type: [{ type: Schema.Types.ObjectId, ref: 'Topic' }],
        select: false
    },
    // 赞过的答案
    likingAnswers: {
        type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
        select: false
    },
    // 踩过的答案
    dislikingAnswers: {
        type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
        select: false
    },
    // 收藏的答案
    collectingAnswers: {
        type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
        select: false
    }
})


// 用户模型
// User 集合名称
// 导出的这个模型也是一个类
module.exports = model('User', userSchema)